package com.ld.wms.business.locker.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.wms.business.locker.mapper.LockerGridHoleMapper;
import com.ld.wms.business.locker.mapper.LockerGridMapper;
import com.ld.wms.business.locker.model.LockerGridHoleModel;
import com.ld.wms.business.locker.model.LockerGridModel;
import com.ld.wms.business.locker.service.LockerGridService;
import com.ld.wms.business.locker.vo.req.LockerGridReqVo;
import com.ld.wms.business.locker.vo.resp.LockerGridHoleRespVo;
import com.ld.wms.business.locker.vo.resp.LockerGridRespVo;
import com.ld.wms.exception.BusinessException;
import com.ld.wms.utils.CommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 智能柜每个格子的信息 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-03-28
 */
@Service
public class LockerGridServiceImpl extends ServiceImpl<LockerGridMapper, LockerGridModel> implements LockerGridService {

    @Autowired
    private LockerGridMapper lockerGridMapper;

    @Autowired
    private LockerGridHoleMapper lockerGridHoleMapper;


    @Override
    @Transactional
    public void configLockerGrid(LockerGridReqVo reqVo) {
        // 如果已经配置过则需要重置
        LockerGridModel lockerGrid = lockerGridMapper.selectById(reqVo.getId());
        if (lockerGrid.getStatus() == 1) {
            throw new BusinessException("重置后才能重新配置！");
        }

        // 将格子状态改成已经配置
        LockerGridModel lockerGridModel = new LockerGridModel();
        BeanUtils.copyProperties(reqVo, lockerGridModel);
        lockerGridModel.setId(reqVo.getId());
        lockerGridModel.setStatus(1);
        lockerGridMapper.updateById(lockerGridModel);
        Integer lockerColumn = reqVo.getLockerColumn();
        Integer lockerRow = reqVo.getLockerRow();
        LockerGridHoleModel lockerGridHoleModel;
        for (int i = 0; i < lockerRow; i++) {
            String constantSingleABC = CommonUtils.getConstantSingleABC(i);
            for (int j = 1; j <= lockerColumn; j++) {
                lockerGridHoleModel = new LockerGridHoleModel();
                lockerGridHoleModel.setLockerGridId(reqVo.getId());
                lockerGridHoleModel.setCode(constantSingleABC + j);
                lockerGridHoleModel.setStatus(0);
                lockerGridHoleMapper.insert(lockerGridHoleModel);
            }
        }
    }

    @Override
    @Transactional
    public void resetLockerGrid(Long id) {
        LockerGridModel lockerGridModel = new LockerGridModel();
        lockerGridModel.setId(id);
        lockerGridModel.setStatus(0);
        lockerGridModel.setLockerColumn(0);
        lockerGridModel.setLockerRow(0);
        lockerGridMapper.updateById(lockerGridModel);
        // 清空抽屉信息
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put(LockerGridHoleModel.LOCKER_GRID_ID, id);
        lockerGridHoleMapper.deleteByMap(columnMap);
    }

    @Override
    public LockerGridRespVo getDetails(Long id) {
        LockerGridRespVo lockerGridRespVo = new LockerGridRespVo();
        LockerGridModel lockerGridModel = lockerGridMapper.selectById(id);
        BeanUtils.copyProperties(lockerGridModel, lockerGridRespVo);
        // 查询抽屉孔信息
        List<LockerGridHoleRespVo> lockerGridHoleRespVoList = lockerGridHoleMapper.selectListByLockerGridId(id);
        lockerGridRespVo.setLockerGridHoleRespVoList(lockerGridHoleRespVoList);
        return lockerGridRespVo;
    }
}
